package com.spt.springbootzhsx.service;

import com.spt.springbootzhsx.dao.MovieDao;
import com.spt.springbootzhsx.dao.OrderDao;
import com.spt.springbootzhsx.dao.UserVipDao;
import com.spt.springbootzhsx.entity.Movie;
import com.spt.springbootzhsx.entity.data.FloatData;
import com.spt.springbootzhsx.entity.data.IntegerData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.crypto.Data;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

@Service
public class DiagramImpl implements DiagramService{
    @Autowired
    private MovieService movieService;
    @Autowired
    private MovieDao movieDao;
    @Autowired
    private UserVipDao userVipDao;
    @Autowired
    private OrderDao orderDao;


    @Override
    public ArrayList<IntegerData> getMstateCount() {
        ArrayList<IntegerData> list=new ArrayList<>();
        IntegerData integerData=new IntegerData();
        Integer ry=movieDao.getMovieCountByMstate("正在热映");
        integerData.setName("正在热映");
        integerData.setValue(ry);
        list.add(integerData);
        IntegerData integerData1=new IntegerData();
        Integer sy=movieDao.getMovieCountByMstate("即将上映");
        integerData1.setName("即将上映");
        integerData1.setValue(sy);
        list.add(integerData1);
        return list;
    }

    @Override
    public ArrayList<IntegerData> getUserTypeCount() {
        IntegerData uCount=new IntegerData();
        IntegerData vCount=new IntegerData();
        ArrayList<IntegerData> list=new ArrayList<>();
        vCount.setName("vip用户");
        vCount.setValue(userVipDao.getVipCount());
        uCount.setName("普通用户");
        uCount.setValue(userVipDao.getUserCount()-vCount.getValue());
        list.add(uCount);
        list.add(vCount);
        return list;
    }

    @Override
    public ArrayList<IntegerData> getMtypeCount() {
        ArrayList<String> list=new ArrayList<>();
        list.add("剧情");
        list.add("家庭");
        list.add("动作");
        list.add("冒险");
        list.add("科幻");
        list.add("爱情");
        list.add("历史");
        list.add("战争");
        ArrayList<IntegerData> list1=new ArrayList<>();
        for (String s : list) {
            IntegerData integerData=new IntegerData();
            integerData.setName(s);
            integerData.setValue(movieDao.getMovieCountByMtype(s));
            list1.add(integerData);
        }
        return list1;
    }

    @Override
    public ArrayList<FloatData> getMovie_Top5Rate() {
        ArrayList<FloatData> list=new ArrayList<>();
        ArrayList<Movie> mlist=movieService.getTop5Movie();
        for (Movie movie : mlist) {
            FloatData floatData=new FloatData();
            floatData.setName(movie.getMname());
            floatData.setValue(movie.getRate());
            list.add(floatData);
        }
        return list;
    }

    @Override
    public ArrayList<IntegerData> getOrder_LastWeek() {
        ArrayList<IntegerData> list=new ArrayList<>();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy-MM-dd");
        Date date=new Date();
        String nowStr=simpleDateFormat.format(date);
        try {
            Date now=simpleDateFormat.parse(nowStr);
            //获取前一周的日期
            Date d1=new Date(now.getTime()-1000*3600*24);
            Date d2=new Date(d1.getTime()-1000*3600*24);
            Date d3=new Date(d2.getTime()-1000*3600*24);
            Date d4=new Date(d3.getTime()-1000*3600*24);
            Date d5=new Date(d4.getTime()-1000*3600*24);
            Date d6=new Date(d5.getTime()-1000*3600*24);
            Date d7=new Date(d6.getTime()-1000*3600*24);
            //查询相关记录条数并添加进list
            IntegerData integerData1=new IntegerData();
            integerData1.setName(simpleDateFormat.format(d7));
            integerData1.setValue(orderDao.getOrderCount_LastSeven()- orderDao.getOrderCount_LastSix());
            list.add(integerData1);
            IntegerData integerData2=new IntegerData();
            integerData2.setName(simpleDateFormat.format(d6));
            integerData2.setValue(orderDao.getOrderCount_LastSix()-orderDao.getOrderCount_LastFive());
            list.add(integerData2);
            IntegerData integerData3=new IntegerData();
            integerData3.setName(simpleDateFormat.format(d5));
            integerData3.setValue(orderDao.getOrderCount_LastFive()- orderDao.getOrderCount_LastFour());
            list.add(integerData3);
            IntegerData integerData4=new IntegerData();
            integerData4.setName(simpleDateFormat.format(d4));
            integerData4.setValue(orderDao.getOrderCount_LastFour()- orderDao.getOrderCount_LastThree());
            list.add(integerData4);
            IntegerData integerData5=new IntegerData();
            integerData5.setName(simpleDateFormat.format(d3));
            integerData5.setValue(orderDao.getOrderCount_LastThree()- orderDao.getOrderCount_LastTwo());
            list.add(integerData5);
            IntegerData integerData6=new IntegerData();
            integerData6.setName(simpleDateFormat.format(d2));
            integerData6.setValue(orderDao.getOrderCount_LastTwo()- orderDao.getOrderCount_LastOne());
            list.add(integerData6);
            IntegerData integerData7=new IntegerData();
            integerData7.setName(simpleDateFormat.format(d1));
            integerData7.setValue(orderDao.getOrderCount_LastOne());
            list.add(integerData7);
        } catch (ParseException e) {
            System.out.println("时间格式有误！");
            e.printStackTrace();
        }
        return list;
    }
}
